<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link
      rel="stylesheet"
      href="./general-styles-of-descendant-components.css"
    />
  </head>
  <body>
    <div id="root"></div>
    <script src="https://cdn.bootcdn.net/ajax/libs/react/17.0.1/umd/react.development.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/react-dom/17.0.1/umd/react-dom.development.min.js"></script>
    <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
    <script type="text/babel">
      const { Component, useState } = React;

      class Demo extends Component {
        state = { count: 0 };
        add = () => {
          this.setState((state) => ({ count: state.count + 1 }));
        };
        render() {
          return (
            <div className="bg">
              <span className="prominent">class组件state</span>
              <h3>当前求和为:{this.state.count}</h3>
              <button onClick={this.add}>点我加1</button>
            </div>
          );
        }
      }
      function Demo1() {
        console.log("Demo1");
        // react 底层处理,第一次是0在内部做缓存,第二次不会覆盖
        const [count, setCount] = useState(0);
        const [name, setName] = useState("tom");
        // 第一种是value字面量
        function add() {
          setCount(count + 1);
        }
        // 第二种是函数
        function reduce() {
          setCount((count) => count - 1);
        }
        function changeName() {
          setName("jack");
        }
        return (
          <div className="bg">
            <span className="prominent">function组件hooks</span>
            <h3>当前求和为:{count}</h3>
            <h3>我的名字是:{name}</h3>
            <button onClick={add}>点我+1</button>
            <button onClick={reduce}>点我-1</button>
            <button onClick={changeName}>点我改名</button>
          </div>
        );
      }
      let vdom = (
        <div>
          <Demo />
          <br />
          <Demo1 />
        </div>
      );
      ReactDOM.render(vdom, document.getElementById("root"));
    </script>
  </body>
</html>
